package com.yeung.interceptors;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * 拦截器会在每一个请求之前,都执行一次 拦截器的方法
 * 需要在spring的上下文中配置
 */
public class SecondInterceptor implements HandlerInterceptor {
    /**
     * 调用目标方法之前被调用
     * 若返回值是true, 则继续调用后续的而拦截器和目标方法
     * 否则, 不会再调用后续的拦截器和目标方法
     * 可以用来做, 权限, 日志,事务
     *
     * @param request
     * @param response
     * @param handler
     * @return
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("[FirstInterceptor] preHandle");
        return true;//true通过拦截器, 执行后续逻辑业务
    }

    /**
     * 调用目标方法之后, 渲染视图之前执行
     * 可以对请求域中的属性或者视图作出修改
     *
     * @param request
     * @param response
     * @param handler
     * @param modelAndView
     * @throws Exception
     */
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        System.out.println("[FirstInterceptor] postHandle");

    }

    /**
     * 视图渲染之后被调用, 释放资源
     *
     * @param request
     * @param response
     * @param handler
     * @param ex
     * @throws Exception
     */
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("[FirstInterceptor] afterCompletion");

    }
}
